<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	/*$member=pdo_getall('jy_jdpm_member',array('uniacid'=>$_SESSION['uniacid']));
	foreach($member as $v){
		$member=get_member($v['mid']);
		$l[]=$member['fromuser'];
	}*/
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	
	if(!empty($_RQ['account'])){
		$params['account like']='%'.$_RQ['account'].'%';
		$return['params']['account']=$_RQ['account'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_jdpm_member',$params,array($pindex,$psize),$total,$fields,'','mid desc');
	
	if($list){
		foreach($list as &$item){
			$member=get_member($item['mid']);
			if($member['fromuser']){
				$item['fromuser']=$member['username'];	
			}else{
				$item['fromuser']='总店';
			}
		$item['nickname']=$member['nickname']?$member['nickname']:'云拍校尉';
		$item['down']=pdo_count('core_members',array('fromuser'=>$item['mid']));
		$item['feeall']=pdo_getsum('jy_jdpm_money_log',array('type'=> 7,'mid'=>$item['mid']),'money');
		$item['feeall']=$item['feeall']['money']?$item['feeall']['money']:'0.00';	
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'分销商管理'),
			
		),
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'mid','title'=>'mid','type'=>'string'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'time'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'account','title'=>'用户账号','type'=>'string'),
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'fromuser','title'=>'上级','type'=>'string'),
			array('name'=>'down','title'=>'下线数','type'=>'string'),
			array('name'=>'feeall','title'=>'佣金总额','type'=>'string'),
		),
		'action'=>array(	
			array('op'=>'down','title'=>'下级用户','type'=>'href'),
			array('op'=>'creditlog','title'=>'推广积分日志','type'=>'href'),
			array('op'=>'clog','title'=>'佣金日志','type'=>'href'),
			
		),
		
		),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'会员UID','name'=>'mid','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'用户账号','name'=>'account','help'=>''),
		),
	);
	
	$return['htmls']=$htmls;
	exi($return);
}

//下级用户
if($_RQ['op']=='down'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>intval($_RQ['mid']));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_members',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			if($item['fromuser']){
				$item['fromuser']=pdo_getcolumn('core_members',array('mid'=>$item['fromuser']),'username');	
			}else{
				$item['fromuser']='总店';
			}
			$item['nickname']=$item['nickname']?$item['nickname']:'云拍校尉';
			$item['feeall']=pdo_getsum('jy_jdpm_money_log',array('type'=> 7,'comuid'=>$item['mid']),'money');
			$item['feeall']=$item['feeall']['money']?$item['feeall']['money']:'0.00';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'分销商管理'),
			
		),
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'mid','title'=>'mid','type'=>'string'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'account','title'=>'用户账号','type'=>'string'),
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'fromuser','title'=>'上级','type'=>'string'),
			array('name'=>'feeall','title'=>'为推荐人贡献佣金','type'=>'string'),
			array('name'=>'createtime','title'=>'推广时间','type'=>'createtime'),
		),
		'action'=>array(	
			array('op'=>'feelog','title'=>'消费记录','type'=>'href'),
		),
		),
	);
	
	$return['htmls']=$htmls;
	exi($return);
}
//积分日志
if($_RQ['op']=='creditlog'){
	
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>intval($_RQ['mid']),'remark'=>'推荐注册送积分');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	//$fields=array('mid','email','avatar','account','nickname','sourceuid');
	$list=pdo_getslice('jy_jdpm_credit_log',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			
			$item['account']=pdo_getcolumn('core_members',array('mid'=>$item['mid']),'username');
			$item['account']=$item['account']?$item['account']:'无';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'积分记录'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('type'=>'string','title'=>'用户UID','name'=>'mid','help'=>''),
			array('name'=>'account','title'=>'用户账号','type'=>'string'),
			
			array('name'=>'fee','title'=>'积分','type'=>'string'),
			array('name'=>'createtime','title'=>'变动时间','type'=>'string'),
			array('name'=>'is_add','title'=>'类型','type'=>'radio','options'=>'is_add'),
			
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			
			
		
		),
		
		),
	);
	
	$return['is_add']=array('1'=>'增加','0'=>'减少');
	$return['htmls']=$htmls;
	exi($return);
}
//佣金日志
if($_RQ['op']=='clog'){
	
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=> 7,'mid'=>intval($_RQ['mid']));
	if(isset($_RQ['pid'])){
		$params['pid']=$_RQ['pid'];
		$return['params']['pid']=$_RQ['pid'];
	}
	if($_RQ['create_time']){
		$time_range=cfc('tools')->base64_decode($_RQ['create_time'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['create_time']=$time_range;
		$params['create_time between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_jdpm_money_log',$params,array($pindex,$psize),$total,$fields,'','create_time desc');
	$fromuser=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['mid'])),'username');
	if($list){
		foreach($list as &$item){
			$item['create_time']=date('Y/m/d H:i:s',$item['create_time']);
			$item['pname']=pdo_getcolumn('jy_jdpm_auction',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']),'pname');
			$item['account']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'username');
			$item['fromuser']=$fromuser;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$total1=pdo_count('jy_jdpm_money_log',$params);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['params']['mid']=$params['mid'];
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'分销佣金日志'),
			
		),
		'export'=>'clogexport',
		'export_title'=>'导出佣金数据',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'mid','title'=>'用户uid','type'=>'string'),
			array('name'=>'account','title'=>'用户账号','type'=>'string'),
			array('name'=>'pname','title'=>'拍卖名称','type'=>'string'),
			array('name'=>'pid','title'=>'拍卖ID','type'=>'string'),
			array('name'=>'money','title'=>'金额','type'=>'string'),
			array('name'=>'comuid','title'=>'下级用户uid','type'=>'string'),
			array('name'=>'create_time','title'=>'创建时间','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			
		
		),
		
		),'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'拍品ID','name'=>'pid','help'=>''),
			array('type'=>'time_range','width'=>'16','title'=>'创建时间','name'=>'create_time','help'=>''),
		),
	);
	
	$return['htmls']=$htmls;
	exi($return);
}
//下级消费日志
if($_RQ['op']=='feelog'){
	$uid=intval($_RQ['mid']);
	$params=array('uniacid'=>$_SESSION['uniacid'],'channel'=> 2,'type'=>array(1,2),'mid'=>$uid);
	if($_RQ['type']){
		$params['type']=$_RQ['type'];
		$return['params']['type']=$_RQ['type'];
	}
	if(isset($_RQ['pid'])){
		$params['pid']=$_RQ['pid'];
		$return['params']['pid']=$_RQ['pid'];
	}
	if($_RQ['create_time']){
		$time_range=cfc('tools')->base64_decode($_RQ['create_time'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['create_time']=$time_range;
		$params['create_time between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_jdpm_money_log',$params,array($pindex,$psize),$total,$fields,'','create_time desc');
	$fromuser=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['mid'])),'username');
	if($list){
		foreach($list as &$item){
			$item['create_time']=date('Y/m/d H:i:s',$item['create_time']);
			$item['pname']=pdo_getcolumn('jy_jdpm_auction',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']),'pname');
			$item['pname']=$item['pname']?$item['pname']:'无';
			$item['account']=pdo_getcolumn('core_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'username');
			$item['fromuser']=$fromuser;
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$total1=pdo_count('jy_jdpm_money_log',$params);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['params']['mid']=$params['mid'];
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'消费日志'),
			
		),
		'export'=>'feelogexport',
		'export_title'=>'导出消费数据',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'mid','title'=>'用户uid','type'=>'string'),
			array('name'=>'account','title'=>'用户账号','type'=>'string'),
			array('name'=>'pname','title'=>'拍卖名称','type'=>'string'),
			array('name'=>'pid','title'=>'拍品ID','type'=>'string'),
			array('name'=>'money','title'=>'金额','type'=>'string'),
			array('name'=>'fromuser','title'=>'上级用户ID','type'=>'string'),
			array('name'=>'create_time','title'=>'变动时间','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
			array('name'=>'remarks','title'=>'备注','type'=>'string'),
		
		),
		
		),'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'拍品ID','name'=>'pid','help'=>''),
			array('type'=>'select','width'=>'15','title'=>'消费类型','name'=>'type','options'=>'type2','select_title'=>'请选择消费类型'),
			array('type'=>'time_range','width'=>'16','title'=>'变动时间','name'=>'create_time','help'=>''),
		),
	);
	$return['type']=$this->feetypes();
	$return['type2']=array(
		array('id'=>1,'title'=>'保证金'),
		array('id'=>2,'title'=>'支付订单'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$item=$this->plugin_setting('commission');
	//print_r($item);exit();
	$htmls=array(
		'type'=>'edit',
		'post'=>'do',
		'desc'=>'分销设置',
		
		'edit'=>array(
			array('name'=>'status','title'=>'是否开启分销','type'=>'radio','options'=>'status'),
			array('name'=>'commission','title'=>'分销比例(%)','type'=>'string','help'=>'平台所得佣金的比例+分销佣金比例=40%'),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='do'){
	$post=get_postdata();
	$item=$this->plugin_setting('commission',$post);
	exi('保存成功');
}
if($_RQ['op']=='clogexport'){
	//导出体现数据
	$uid=intval($_RQ['mid']);
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=> 7,'mid'=>intval($_RQ['mid']));
	if(isset($_RQ['pid'])){
		$params['pid']=$_RQ['pid'];
		$return['params']['pid']=$_RQ['pid'];
	}
	if($_RQ['create_time']){
		$time_range=cfc('tools')->base64_decode($_RQ['create_time'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['create_time']=$time_range;
		$params['create_time between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_jdpm_money_log',$params,array(),'','create_time desc');
	if($list){
		foreach($list as &$item){
			$item['create_time']=date('Y/m/d H:i:s',$item['create_time']);
			$item['pname']=pdo_getcolumn('jy_jdpm_auction',array('pid'=>$item['pid']),'pname');
			$item['pname']=$item['pname']?$item['pname']:'无';
			$item['account']=pdo_getcolumn('jy_jdpm_member',array('mid'=>$item['mid']),'account');
			$item['type']=$this->feetypes()[$item['type']];
		}
	}
	$header=array(
		'mid'=>'用户UID',
		'account'=>'用户账号',
		'pname'=>'拍品名称',
		'pid'=>'拍品ID',
		'money'=>'金额',
		'comuid'=>'下级用户ID',
		'remarks'=>'备注',
		'create_time'=>'时间',
	);
	cfc('tools')->export2excel($header,$list,$uid.'用户佣金数据'.date('Y/m/d',time()));
}
if($_RQ['op']=='feelogexport'){
	//导出体现数据
	$uid=intval($_RQ['mid']);
	$params=array('uniacid'=>$_SESSION['uniacid'],'channel'=> 2,'mid'=>$uid);
	if($_RQ['type']){
		$params['type']=$_RQ['type'];
		$return['params']['type']=$_RQ['type'];
	}
	if(isset($_RQ['pid'])){
		$params['pid']=$_RQ['pid'];
		$return['params']['pid']=$_RQ['pid'];
	}
	if($_RQ['create_time']){
		$time_range=cfc('tools')->base64_decode($_RQ['create_time'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['create_time']=$time_range;
		$params['create_time between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_jdpm_money_log',$params,array(),'','create_time desc');
	$fromuser=pdo_getcolumn('core_members',array('mid'=>intval($_RQ['mid'])),'username');
	if($list){
		foreach($list as &$item){
			$item['create_time']=date('Y/m/d H:i:s',$item['create_time']);
			$item['pname']=pdo_getcolumn('jy_jdpm_auction',array('id'=>$item['pid']),'pname');
			$item['pname']=$item['pname']?$item['pname']:'无';
			$item['account']=pdo_getcolumn('core_members',array('id'=>$item['mid']),'username');
			$item['fromuser']=$fromuser;
			$item['type']=$this->feetypes()[$item['type']];
		}
	}
	$header=array(
		'mid'=>'用户UID',
		'account'=>'用户账号',
		'pname'=>'拍品名称',
		'pid'=>'拍品ID',
		'money'=>'金额',
		'fromuser'=>'上级用户ID',
		'type'=>'类型',
		'remarks'=>'备注',
		'create_time'=>'时间',
	);
	cfc('tools')->export2excel($header,$list,$uid.'用户消费数据'.date('Y/m/d',time()));
}